import { AnyObject } from 'antd/es/_util/type';

/**
 * 设置localStorage内容
 */
export function setLocalStorage(key: string, value: string) {
  localStorage.setItem(key, JSON.stringify(value));
}
/**
 * 获取localStorage内容
 */
export function getLocalStorage(key: string) {
  return isEmpty(localStorage.getItem(key))?localStorage.getItem(key) :JSON.parse(localStorage.getItem(key) || `{}`);
}

/**
 *
 * @param o 判断o是否为空
 * @returns
 */
export function isEmpty(o) {
  return o === undefined || o === '' || o === null;
}

/**
 * @description 对象键值转对象数组
 *
 */
export function objToArr(obj: AnyObject) {
  return Object.entries(obj).map(([key, value]) => {
    const targetKey = isNaN(Number(key)) ? key : Number(key);
    return { key: targetKey, value };
  });
}


// 获取文件后缀名
export function getFileExecName(url:string) {
  let temp = url.split('?');
  temp = temp[0].split('/');
  const filename = temp[temp.length - 1];
  const filenameWithoutSuffix = filename.split(/#|\?/)[0];
  return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
}


/**
 * 
 * @param url 图片地址
 * @returns  图片链接
 */
export const getImgUrl = (url: string) => {
  return `http://8.137.109.35:9001/blog-bucket/${url}`;
};


// /**
// * @description
// * 构造敏感词map
// * @private
// * @returns
// */
// export const  makeSensitiveMap =(sensitiveWordList:string[])=> {
//   // 构造根节点
//   const result = new Map();
//   for (const word of sensitiveWordList) {
//       let map = result;
//       for (let i = 0; i < word.length; i++) {
//           // 依次获取字
//           const char = word.charAt(i);
//           // 判断是否存在
//           if (map.get(char)) {
//               // 获取下一层节点
//               map = map.get(char);
//           } else {
//               // 将当前节点设置为非结尾节点
//               if (map.get('laster') === true) {
//                   map.set('laster', false);
//               }
//               const item = new Map();
//               // 新增节点默认为结尾节点
//               item.set('laster', true);
//               map.set(char, item);
//               map = map.get(char);
//           }
//       }

//   }
//   return result;
// }


// /**
// * @description
// * 检查敏感词是否存在
// * @private
// * @param {any} txt
// * @param {any} index
// * @returns
// */
// export const checkSensitiveWord =(sensitiveMap, txt, index)=> {
//   let currentMap = sensitiveMap;
//   let flag = false;
//   let wordNum = 0;//记录过滤
//   let sensitiveWord = ''; //记录过滤出来的敏感词
//   for (let i = index; i < txt.length; i++) {
//       const word = txt.charAt(i);
//       currentMap = currentMap.get(word);
//       if (currentMap) {
//           wordNum++;
//           sensitiveWord += word;
//           if (currentMap.get('laster') === true) {
//               // 表示已到词的结尾
//               flag = true;
//               break;
//           }
//       } else {
//           break;
//       }
//   }
//   // 两字成词
//   if (wordNum < 2) {
//       flag = false;
//   }
//   return flag ? sensitiveWord :void 0;
// }
// /**
// * @description
// * 判断文本中是否存在敏感词
// * @param {any} txt
// * @returns
// */
// export const filterSensitiveWord =(txt, sensitiveMap) => {
//   let matchResult:string[] = [];
//   // 过滤掉除了中文、英文、数字之外的
//   const txtTrim = txt.replace(/[^\u4e00-\u9fa5\u0030-\u0039\u0061-\u007a\u0041-\u005a]+/g, '');
//   for (let i = 0; i < txtTrim.length; i++) {
//       const res = checkSensitiveWord(sensitiveMap, txtTrim, i)
//       if(res)matchResult.push(res);
//   }
//   return matchResult;
// }


type TrieNode = Map<string, TrieNode | boolean>;

export const makeSensitiveMap = (sensitiveWordList: string[]): TrieNode => {
  const result: TrieNode = new Map();
  for (const word of sensitiveWordList) {
    let map: TrieNode = result;
    for (const char of word) {
      if (!map.has(char)) {
        map.set(char, new Map().set('laster', false));
      }
      map = map.get(char) as TrieNode;
    }
    map.set('laster', true);
  }
  return result;
};

export const checkSensitiveWord = (sensitiveMap: TrieNode, txt: string, index: number): string | void => {
  let currentMap: TrieNode | boolean | undefined = sensitiveMap;
  let wordNum = 0;
  let sensitiveWord = '';

  for (let i = index; i < txt.length; i++) {
    const word = txt.charAt(i);
    currentMap = currentMap?.get(word);
    if (currentMap) {
      wordNum++;
      sensitiveWord += word;
      if (currentMap === true) {
        return sensitiveWord;
      }
    } else {
      break;
    }
  }

  // 两字成词
  return wordNum >= 2 ? sensitiveWord : void 0;
};

export const filterSensitiveWord = (txt: string, sensitiveMap: TrieNode): string[] => {
  const matchResult = new Set();

  for (let i = 0; i < txt.length; i++) {
    const char = txt.charAt(i);
    // 只检查中文、英文、数字字符
    if (/[\u4e00-\u9fa5\u0030-\u0039\u0061-\u007a\u0041-\u005a]/.test(char)) {
      const res = checkSensitiveWord(sensitiveMap, txt, i);
      if (res) matchResult.add(res);
    }
  }

  return matchResult;
};